package com.shuzijun.leetcode.editor.cn;

import java.util.ArrayList;

public class FindClosestLcci{
    public static void main(String[] args) {
         Solution solution = new FindClosestLcci().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int findClosest(String[] words, String word1, String word2) {
        ArrayList<Integer> w1ps = new ArrayList<>();
        ArrayList<Integer> w2ps = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (word.equals(word1)){
                w1ps.add(i);
            } else if (word.equals(word2)) {
                w2ps.add(i);
            }
        }

        int p1 = 0;
        int p2 = 0;
        int maxValue = Integer.MAX_VALUE;
        while (p1 < w1ps.size() && p2 < w2ps.size()){
            Integer pos1 = w1ps.get(p1);
            Integer pos2 = w1ps.get(p2);
            if (pos1 > pos2) {
                if (maxValue > pos1-pos2){
                    maxValue = pos1-pos2;
                }
                p2++;
            }else{
                if (maxValue > pos2-pos1){
                    maxValue = pos2-pos1;
                }
                p1++;
            }
        }

        return maxValue;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}